import { onShow } from '@dcloudio/uni-app'
import { useCartStore } from '@/store/cart'
import { useUserInfo } from '@/store/user'
import { watch } from 'vue'

const store = useCartStore()
const userStore=useUserInfo()

export const useTarBarBadge = () => {
  onShow(() => {
    if (store.getAcount > 0) {
      uni.setTabBarBadge({
        index: 2,
        text: store.getAcount + ''
      })
    } else {
      uni.removeTabBarBadge({
        index: 2
      })
    }
  })
  watch(
    () => store.getAcount,
    newVal => {
      if (newVal > 0) {
        uni.setTabBarBadge({
          index: 2,
          text: newVal + ''
        })
      } else {
        uni.removeTabBarBadge({
          index: 2
        })
      }
    },
    {
      immediate: true
    }
  )
}
//设置tabBar第四项的文字
export const useTabBarTitle = () => {
  onShow(() => {
    if (userStore.token) {
      uni.setTabBarItem({
        index: 3,
        text: '我的'
      })
    } else {
      uni.setTabBarItem({
        index: 3,
        text: '未登录'
      })
    }
  })
}
